package com.wang.wangaiagent.advisor;


import com.wang.wangaiagent.app.LoveApp;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.advisor.api.*;
import reactor.core.publisher.Flux;



/**
 * CallAroundAdvisor 用于处理同步请求和响应 (非流式)
 * StreamAroundAdvisor 用于处理流式请求和响应
 * 自定义 Advisor 时建议同时实现这两个类.
 *
 * @author wang yandong
 *
 */
public class MyCustomAdvisor implements CallAroundAdvisor, StreamAroundAdvisor {

    @NotNull
    @Override
    public AdvisedResponse aroundCall(@NotNull AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {
        // 1. 处理请求 (前置处理)
        AdvisedRequest modifiedRequest = processRequest(advisedRequest);

        // 2. 调用链中的下一个Advisor (如果有)
        //assert modifiedRequest != null;
        AdvisedResponse response = chain.nextAroundCall(modifiedRequest);

        // 3. 处理响应 (后置处理)
        return processResponse(response);
    }

    @NotNull
    @Override
    public Flux<AdvisedResponse> aroundStream(@NotNull AdvisedRequest advisedRequest, StreamAroundAdvisorChain chain) {
        // 1. 处理请求
        AdvisedRequest modifiedRequest = processRequest(advisedRequest);

        // 2. 调用链中的下一个Advisor并处理流式响应
        assert modifiedRequest != null;
        return chain.nextAroundStream(modifiedRequest)
                .map(response -> processResponse(response));
    }

    private AdvisedRequest processRequest(AdvisedRequest advisedRequest) {
        // 定义自己的处理逻辑
        return null;
    }
    private AdvisedResponse processResponse(AdvisedResponse advisedResponse) {
        // 定义自己的处理逻辑
        return advisedResponse;
    }


    @NotNull
    @Override
    public String getName() {
        return "wangyandong自定义的拦截器";
    }

    @Override
    public int getOrder() {
        // 数字越小优先级越高 (越先执行)
        return 0;
    }
}

